<?php
namespace app\admin\controller;
use think\Controller;
use think\Validate;
use think\Db;
use think\Session;
class Operate extends Basic
{  

	
    public function listf()
    {
    $list=Db::name('ratype')->select();
	$this->assign('list',$list);
     return $this->fetch();
    }
	 public function uplistf()
    {
  $post=input('post.');
  if($post){
  
  $id=$post['id'];
  if($post['title']==''){$this->error('分类名不能为空');}
  if($id!=''){
      $list=Db::name('ratype')->where('id',$id)->find();
      if(!$list){$this->error('不存在此分类');}
      $list=Db::name('ratype')->where('title',$post['title'])->find();
	  if($list){$this->error('该分类名已存在');}
      Db::name('ratype')->where('id',$id)->update($post);
	  $this->success('编辑成功');
	 }else{
	 
	  $list=Db::name('ratype')->where('title',$post['title'])->find();
	  if($list){$this->error('该分类名已存在');}
	    Db::name('ratype')->insert($post);
	  $this->success('编辑成功');

	 }
  }else{
	  if(empty(input('param.id'))){
	  $list['title']='';
	   $list['id']='';
	  }else{
	$id=input('param.id');
    $list=Db::name('ratype')->where('id',$id)->find();
	
	}
	$this->assign('list',$list);
     return $this->fetch();
	 
	 }
    }
	 public function delistf()
    {
			$id=input('param.id');
    $list=Db::name('ratype')->where('id',$id)->select();
	 if(!$list){$this->error('不存在此分类');}
     Db::name('ratype')->where('id',$id)->delete();
	 $this->success('删除成功');
    }




	public function article()
    {
 $post=input('post.');
   $list=Db::name('ratype')->select();
	$this->assign('fl',$list);
	$tt['flid']='';
	$tt['titlef']='';
  if(!empty($post['flid']) or !empty($post['titlef'])){
	 
     if(!empty($post['flid'])){$date['flid']=$post['flid'];$tt['flid']=$post['flid'];}
	 if(!empty($post['titlef'])){$date['titlef']=$post['titlef'];$tt['titlef']=$post['titlef'];}
	 
	  $list=Db::name('article')->alias('a')->join('__RATYPE__ w','a.flid=w.id')->order('id desc')->where($date)->field('a.*,w.title')->paginate(10);
     }else{
   
    $list=Db::name('article')->alias('a')->join('__RATYPE__ w','a.flid=w.id')->order('id desc')->field('a.*,w.title')->paginate(10);}
	
	$this->assign('list',$list);
	$this->assign('date',$tt);
     return $this->fetch();
    }

	public function uparticle()
    {
   $post=input('post.');
  
  if($post){
	
   if($post['type']==''){$this->error('内容不能为空');}
    if($post['titlef']==''){$this->error('标题不能为空');}
	unset($post['editorValue']);

      $id=$post['id'];
  
      if($id!=''){

         $list=Db::name('article')->where('id',$id)->find();
      if(!$list){$this->error('不存在此文章');}
     
      Db::name('article')->where('id',$id)->update($post);
	  $this->success('编辑成功');
	 }else{
	 
	   
	    Db::name('article')->insert($post);
	    $this->success('编辑成功');

	 }
  }else{
	  if(empty(input('param.id'))){
	   $list['titlef']='';
	   $list['id']='';
	   $list['type']='';
	   $list['flid']='';

	  }else{
	$id=input('param.id');
    $list=Db::name('article')->where('id',$id)->find();
	
	}
	 $listf=Db::name('ratype')->select();
	  $this->assign('fl',$listf);
	$this->assign('list',$list);
     return $this->fetch();
	 
	 }
    }
	
	 public function dearticle()
    {
			$id=input('param.id');
    $list=Db::name('article')->where('id',$id)->select();
	 if(!$list){$this->error('不存在此文章');}
     Db::name('article')->where('id',$id)->delete();
	 $this->success('删除成功');
    }
}
